//
//  M1ColumnDetailsVC.m
//  NewMarryU
//
//  Created by Cedar on 2018/7/22.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1ColumnDetailsVC.h"
#import "M1ColumnModel.h"
#import "M1DragContainer.h"
#import "M1CustomCardView.h"
#import "M1HomeDetailsVC.h"
#import "M1HomeModel.h"
#import "M1PhotoGalleryVC.h"
#import "M1PersonalDataVC.h"

@interface M1ColumnDetailsVC ()<CCDraggableContainerDataSource,CCDraggableContainerDelegate
>
{
    NSInteger m1_order_rand;
    NSInteger m1_page;
}

@property (nonatomic, strong) NSString *m1_max_time;

@property (nonatomic, strong) M1DragContainer *m1_container;

@property (nonatomic, strong)  NSMutableArray *m1_requestIndexs;

@property (nonatomic, strong) UIView *m1_empty;

@end

@implementation M1ColumnDetailsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = self.m1_model.title;
    
    _m1_requestIndexs = [NSMutableArray arrayWithCapacity:0];
    
    self.m1_listData = [NSMutableArray arrayWithCapacity:0];
    
    [self m1_createEmptyView];
        
    [self m1_loadUI];
    
    [self m1_reloadData];
    
    [self m1_requestData];
}

- (void)m1_requestData{
    
    NSDictionary *params = @{@"page":@(m1_page),
                             @"pid":self.m1_model.pid,
                             @"order_rand":@(m1_order_rand)
                             };
    WEAKSELF
    [M1HttpNet requestWithMethod:m1_user_prefecture_list_url params:params success:^(id resDic, NSInteger code, NSString *message) {
        weakSelf.m1_max_time = [resDic m1_stringKey:@"max_time"];
        if (weakSelf.m1_listData.count > 0) {
            [weakSelf.m1_listData addObjectsFromArray:resDic[@"user"]];
        }else{
            [weakSelf.m1_listData addObjectsFromArray:resDic[@"user"]];
            [weakSelf.m1_container reloadData];
        }
        
        if (weakSelf.m1_listData.count == 0) {
            weakSelf.m1_empty.hidden = NO;
        }else{
            weakSelf.m1_empty.hidden = YES;
        }
        
    } fail:^(id resDic, NSInteger code, NSString *message) {
        
    } showAni:YES];
}


#pragma mark - CCDraggableContainer DataSource
- (M1DragCardView *)draggableContainer:(M1DragContainer *)draggableContainer viewForIndex:(NSInteger)index {
    
    M1CustomCardView *cardView = [[M1CustomCardView alloc] initWithFrame:draggableContainer.bounds];
    
    cardView.m1_viewController = self;
    
    M1HomeModel *model = [M1HomeModel mj_objectWithKeyValues:[self.m1_listData objectAtIndex:index]];
    
    [cardView installData:model];
    
    if (index == self.m1_listData.count-7 ||index == self.m1_listData.count-1) {
        
        if ([_m1_requestIndexs indexOfObject:@(index)]!=NSNotFound) {
        }else{
            m1_page++;
            if (m1_order_rand == 5) {
                m1_order_rand = 1;
            }else{
                m1_order_rand++;
            }
            [_m1_requestIndexs addObject:@(index)];
            [self m1_requestData];
        }
    }
    
    return cardView;
}

- (NSInteger)numberOfIndexs {
    return self.m1_listData.count;
}

#pragma mark - CCDraggableContainer Delegate
- (void)draggableContainer:(M1DragContainer *)draggableContainer draggableDirection:(CCDraggableDirection)draggableDirection currentCardsCount:(NSInteger)currentCardsCount didRemoveView:(BOOL)didRemoveView{
    M1CustomCardView *view = self.m1_container.currentCards.firstObject;
    
    NSLog(@"%ld", view.tag);
    
    if (draggableDirection == CCDraggableDirectionLeft) {
        NSLog(@"左");
        
    }
    
    if (draggableDirection == CCDraggableDirectionRight) {
        NSLog(@"右");
        NSInteger m1_index = view.tag;
        if (view.tag<=0) {
            m1_index = self.m1_listData.count-1;
        }
        M1HomeModel *model = [M1HomeModel mj_objectWithKeyValues:[self.m1_listData objectAtIndex:m1_index]];
        WEAKSELF
        [M1HttpNet requestWithMethod:m1_user_like_url params:@{@"to_uid":model.basic.uid} success:^(id resDic, NSInteger code, NSString *message) {
        } fail:^(id resDic, NSInteger code, NSString *message) {
            if (code == -20001 || code == -20002 || code == -20003) {
                [M1Alert m1_alertWithTitle:@"" message:message buttonTitles:@[@"取消", @"立即完善"] action:^(NSInteger index) {
                    if (index == 1) {
                        if ([[M1User user].basic.avatar.img.url length] <= 0 ||[M1User user].basic.photos.count<=0) {
                            M1PhotoGalleryVC *photoVC = [[M1PhotoGalleryVC alloc] init];
                            photoVC.hidesBottomBarWhenPushed = YES;
                            [weakSelf.navigationController pushViewController:photoVC animated:YES];
                        }else{
                            M1PersonalDataVC *dataVC = [[M1PersonalDataVC alloc] init];
                            dataVC.hidesBottomBarWhenPushed = YES;
                            [weakSelf.navigationController pushViewController:dataVC animated:YES];
                        }
                    }
                }];
            }else if (code >-20000 &&code<=-10000){
                //会员
                [M1Alert m1_alertWithTitle:nil message:message buttonTitles:@[@"取消", @"前往"] action:^(NSInteger index) {
                    if (index == 1) {
                        //会员
                        M1VIPCenterVC *m1_vipVC = [[M1VIPCenterVC alloc]init];
                        m1_vipVC.hidesBottomBarWhenPushed = YES;
                        [weakSelf.navigationController pushViewController:m1_vipVC animated:YES];
                    }
                }];
 
            }
        } showAni:NO];
    }
    
    if (view.tag<=0) {
        WEAKSELF
        [M1Alert m1_alertWithTitle:nil message:@"没有更多数据了" buttonTitles:@[@"确定"] action:^(NSInteger index) {
            weakSelf.m1_empty.hidden = NO;
        }];
    }
    
    if (view.tag == 0) {
        NSLog(@"最后一张");
//        [draggableContainer reloadData];
    }
}

- (void)draggableContainer:(M1DragContainer *)draggableContainer cardView:(M1DragCardView *)cardView didSelectIndex:(NSInteger)didSelectIndex {
    
    NSLog(@"点击了Tag为%ld的Card", (long)didSelectIndex);
    
    M1HomeModel *model = [M1HomeModel mj_objectWithKeyValues:[self.m1_listData objectAtIndex:didSelectIndex]];
    
    M1HomeDetailsVC *detailsVC = [[M1HomeDetailsVC alloc] initWithStyle:UITableViewStyleGrouped];
    detailsVC.m1_model = model;
    detailsVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailsVC animated:YES];
    
}

- (void)draggableContainer:(M1DragContainer *)draggableContainer draggableDirection:(CCDraggableDirection)draggableDirection widthRatio:(CGFloat)widthRatio heightRatio:(CGFloat)heightRatio {
    if (draggableDirection == CCDraggableDirectionLeft) {
        //        NSLog(@"左");
        
    }
    
    if (draggableDirection == CCDraggableDirectionRight) {
        //        NSLog(@"右");
    }
}

- (void)draggableContainer:(M1DragContainer *)draggableContainer finishedDraggableLastCard:(BOOL)finishedDraggableLastCard {
    
    
//        [draggableContainer reloadData];
}

- (void)m1_loadUI{
    self.m1_container = [[M1DragContainer alloc] initWithFrame:CGRectMake(15, m1_NAV_HEIGHT + (SCREENHEIGHT - m1_NAV_HEIGHT - 20 - (iPhone5?430:530)) / 2, SCREENWIDTH - 30, iPhone5?430:530) style:CCDraggableStyleUpOverlay];
    self.m1_container.delegate = self;
    self.m1_container.dataSource = self;
    [self.view addSubview:self.m1_container];
}

- (void) m1_reloadData{
    m1_page = 1;
    m1_order_rand = 1;
    self.m1_max_time = @"0";
}

- (void)m1_createEmptyView{
    self.m1_empty = [[UIView alloc] init];
    _m1_empty.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_m1_empty];
    _m1_empty.hidden = YES;
    
    [_m1_empty mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    UIImageView *image = [[UIImageView alloc] init];
    image.image = LOAD_LOCAL_IMG(@"data_none");
    [_m1_empty addSubview:image];
    
    [image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view.mas_centerY);
        make.centerX.equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(100, 100));
    }];
    
    UILabel *title = [[UILabel alloc] init];
    title.text = @"空空如也~";
    title.textColor = m1_color(0x666666);
    title.font = m1_font(15);
    [_m1_empty addSubview:title];
    
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(image.mas_bottom).offset(10);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
